//
//  ZSHomeViewController.swift
//  creativeassistant
//
//  Created by 灵工邦 on 2025/2/8.
//

import UIKit

class ZSHomeViewController: ZSBaseViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        configSubscription()
    }
    
    func configSubscription() {
        YSIAPManager.shared().checkOrderState {} failure: {}
        let status: Bool = UserDefaults.standard.bool(forKey: isFirstOpen)
        if !status {
            UserDefaults.standard.setValue(true, forKey: isFirstOpen)
            goSubscription()
        }
    }
    
    func goSubscription() {
        let vc = ZSSubscriptionViewController()
        vc.modalPresentationStyle = .overFullScreen
        self.present(vc, animated: false)
    }
    
    @IBAction func goAIAssistant(_ sender: UIButton) {
        if let orderid = UserDefaults.standard.object(forKey: "orderId") as? String, !orderid.isEmpty  {
            let vc = ZSAssistantViewController()
            switch sender.tag {
            case 1001:
                vc.aiIdentity = 1
            case 1002:
                vc.aiIdentity = 2
            case 1003:
                vc.aiIdentity = 3
            default:
                break
            }
            self.navigationController?.pushViewController(vc, animated: true)
            return
        }
        if let benefit = UserDefaults.standard.object(forKey: zsBenefit) as? Date, DateUtils.isDateWithinThreeDaysBeforeToday(benefit)  {
            let vc = ZSAssistantViewController()
            switch sender.tag {
            case 1001:
                vc.aiIdentity = 1
            case 1002:
                vc.aiIdentity = 2
            case 1003:
                vc.aiIdentity = 3
            default:
                break
            }
            self.navigationController?.pushViewController(vc, animated: true)
            return
        }
        if var signData = UserDefaults.standard.object(forKey: zsSignData) as? Dictionary<String, Any> {
            if let number = signData["number"] as? Int, number > 0 {
                signData["number"] = number - 1
                UserDefaults.standard.setValue(signData, forKey: zsSignData)
                let vc = ZSAssistantViewController()
                switch sender.tag {
                case 1001:
                    vc.aiIdentity = 1
                case 1002:
                    vc.aiIdentity = 2
                case 1003:
                    vc.aiIdentity = 3
                default:
                    break
                }
                self.navigationController?.pushViewController(vc, animated: true)
                return
            }
        }
        goSubscription()
    }
    
    @IBAction func goWeChatMomentsCopywriting(_ sender: UIButton) {
        if let orderid = UserDefaults.standard.object(forKey: "orderId") as? String, !orderid.isEmpty  {
            let vc = ZSWechatTimeLineViewController()
            self.navigationController?.pushViewController(vc, animated: true)
            return
        }
        if let benefit = UserDefaults.standard.object(forKey: zsBenefit) as? Date, DateUtils.isDateWithinThreeDaysBeforeToday(benefit)  {
            let vc = ZSWechatTimeLineViewController()
            self.navigationController?.pushViewController(vc, animated: true)
            return
        }
        if var signData = UserDefaults.standard.object(forKey: zsSignData) as? Dictionary<String, Any> {
            if let number = signData["number"] as? Int, number > 0 {
                signData["number"] = number - 1
                UserDefaults.standard.setValue(signData, forKey: zsSignData)
                let vc = ZSWechatTimeLineViewController()
                self.navigationController?.pushViewController(vc, animated: true)
                return
            }
        }
        goSubscription()
    }
    
    @IBAction func goXiaohongshu(_ sender: UIButton) {
        if let orderid = UserDefaults.standard.object(forKey: "orderId") as? String, !orderid.isEmpty  {
            let vc = ZSHotSellingCopywritingViewController()
            self.navigationController?.pushViewController(vc, animated: true)
            return
        }
        if let benefit = UserDefaults.standard.object(forKey: zsBenefit) as? Date, DateUtils.isDateWithinThreeDaysBeforeToday(benefit)  {
            let vc = ZSHotSellingCopywritingViewController()
            self.navigationController?.pushViewController(vc, animated: true)
            return
        }
        if var signData = UserDefaults.standard.object(forKey: zsSignData) as? Dictionary<String, Any> {
            if let number = signData["number"] as? Int, number > 0 {
                signData["number"] = number - 1
                UserDefaults.standard.setValue(signData, forKey: zsSignData)
                let vc = ZSHotSellingCopywritingViewController()
                self.navigationController?.pushViewController(vc, animated: true)
                return
            }
        }
        goSubscription()
    }
    
    @IBAction func goVideoScript(_ sender: Any) {
        if let orderid = UserDefaults.standard.object(forKey: "orderId") as? String, !orderid.isEmpty  {
            let vc = ZSVideoScriptViewController()
            self.navigationController?.pushViewController(vc, animated: true)
            return
        }
        if let benefit = UserDefaults.standard.object(forKey: zsBenefit) as? Date, DateUtils.isDateWithinThreeDaysBeforeToday(benefit)  {
            let vc = ZSVideoScriptViewController()
            self.navigationController?.pushViewController(vc, animated: true)
            return
        }
        if var signData = UserDefaults.standard.object(forKey: zsSignData) as? Dictionary<String, Any> {
            if let number = signData["number"] as? Int, number > 0 {
                signData["number"] = number - 1
                UserDefaults.standard.setValue(signData, forKey: zsSignData)
                let vc = ZSVideoScriptViewController()
                self.navigationController?.pushViewController(vc, animated: true)
                return
            }
        }
        goSubscription()
    }
    
}
